Hands On: Modifying a Report

Now that you've created a simple report, you'll modify some of its properties and make it more interactive by adding some event driven code. This exercise only touches on a few of the hundreds of properties and methods available but will give you an idea of how simple it is to fully integrate and customize  reports in your Visual Basic projects.

Step by step

1 If not already open, open the Visual Basic 5.0/6.0 application and the report project you created in the last exercise.
2 This sample report is currently based on all regions. You’ll now modify the selection criteria to include only data from the USA. But first you may need to add a reference to your project. Navigate to the Project menu in Visual Basic and select References…
In the Available References text box, locate the "Microsoft ActiveX Data Objects Recordset x.x Library" and, if it isn’t already checked, click the check box on and click OK. You shouldn’t need to do this again.
3 Open the form with the Smart Viewer ("Form2") with the code for editing in the main window. It should look something like this:

Dim Report As New CrystalReport1

Private Sub Form_Load()
CRViewer1.ReportSource = Report
CRViewer1.ViewReport

End Sub

Private Sub Form_Resize()
CRViewer1.Top = 0
CRViewer1.Left = 0
CRViewer1.Height = ScaleHeight
CRViewer1.Width = ScaleWidth

End Sub

4 You’ll now add a new selection criteria to the report. Edit the "Form2" declarations and "Form_Load" procedure to look like the following:

Dim Report As New CrystalReport1
Dim rs As New ADOR.Recordset

Private Sub Form_Load()
Rs.Open "Select * from customer where country = 'USA'", "xtreme sample data"
Report.Database.SetDataSource rs

CRViewer1.ReportSource = Report
CRViewer1.ViewReport

End Sub

This will add a query to select only the records with "USA" in the "Country" field. This recordset will be passed to the report engine and the results displayed. Run the report again to see the changes.
Instead of the selection string shown above, you could replace the "USA" with a string variable which is set by your application. You could even build the entire selection formula using code to produce customized reports from the generic sales report you created.
5 Now you’ll add a text object to the report and manipulate it using code. In the main Report Designer window, right click just to the right of the graph in the Report Header pane and select Insert and Text Object. Move the text object to just above the pie graph. You may have to move the graph down a bit first. Make note of the Name in the Properties is window. In this example, the name is "Text7" but the name in your project may be different.
6 You’ll now set the contents of the text object you just added. Open the "Form2" code for editing in the main window. Add some code to the "Form2" "Form_Load" procedure just before the reference to the CRViewer as shown below:

Dim Report As New CrystalReport1
Dim rs As New ADOR.Recordset


Private Sub Form_Load()
rs.Open "Select * from customer where country = 'USA'", "xtreme sample data"
Report.Database.SetDataSource rs

Report.Text7.SetText "Here is some text from my app"
Report.Text7.Font.Italic = True
Report.Text7.Font.Bold = True
Report.Text7.Font.Size = 14

CRViewer1.ReportSource = Report
CRViewer1.ViewReport

End Sub

This will set the text object, change the font to Italic, Bold 14 point and display the results. Run the report again to see the changes.
7 To complete this exercise, you’ll now add some event driven code to explore how you can make the run time viewer control more interactive.
Using the Object and Procedure list boxes at the top of the code window, select the "CRViewer1" object and add the "DrillOnGroup" and "PrintButtonClicked" procedures to the "Form2" code. Edit these new procedures as shown below:

Private Sub CRViewer1_DrillOnGroup(GroupNameList As Variant, ByVal DrillType As
CRVIEWERLibCtl.CRDrillType, UseDefault As Boolean)
MsgBox "You're drilling down on the " & GroupNameList(0) & " group!"

End Sub

Private Sub CRViewer1_PrintButtonClicked(UseDefault As Boolean)
MsgBox "You clicked the Print button!"

End Sub

The "DrillOnGroup" procedure is triggered when the user double-clicks on any graph pie slice or report summary field. When this occurs, the text shown will appear in a message box. The "PrintButtonClicked" procedure works similarly when the viewer Print button is clicked.
A practical use of these events would be to display custom dialogs or process other code which would replace or modify the standard viewer behavior.

Now that you’ve seen a few simple ways to modify a report, you may want to explore some of the other properties and events in the report engine that you can use in your application. For other examples or more details you may also want to consult the Help file.


For the latest information about this product, please visit the web site at www.seagatesoftware.com/scrvbasic.

Copyright ⌐ 1998 Seagate Software Inc. All rights reserved. Click here for additional information.